import Ember from 'ember';
import BaseItem from '../ui/base-ui-item';
import Echarts from "npm:echarts";
export default Ember.Component.extend({
    textShow: true,
    informationShow: true,
    bloodShow: false, //血压显示
    heartShow: false, //心率显示
    allDateShow: true, //显示所有时间
    todayShow: false, //显示今天
    sevenShow: false, //显示一周
    thirtyShow: false, //显示一个月
    compareShow: false, //自定义日期
    constants: Constants,
    itemtypeID: "",
    itemtypeCode:"",
    itemtype:null,
    statusService: Ember.inject.service("current-status"),
    store: Ember.inject.service("store"),
    dateService: Ember.inject.service("date-service"),
    pathConfiger: Ember.inject.service("path-configer"),
    // init() {
    //     this._super(...arguments);
    //     let model = this.get("store").createRecord('health-info', {});
    //     // this.set("customerInComp",model);
    //     this.health = model;
    // },
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.customer-info');
        //route.refresh();
        App.lookup('controller:business.mainpage').refreshPage(route);
    },
    itemTypelObs:function(){
      var _self=this;
      var itemtypeCode=this.get("itemtypeCode");
      var filter = {};
      if (this.get('customerInComp.id')) {
          filter = $.extend({}, filter, {
              '[examUser][id]': this.get('customerInComp.id')
          });
      }
      if(itemtypeCode){
        filter = $.extend({}, filter, {
            '[itemtype][typecode]': itemtypeCode
        });
        this.get("store").query('health-info', {
            filter: filter,
            sort: {
                '[examDateTime]': 'asc'
            },
        }).then(function(bloodList) {
            _self.set('lastHealth', bloodList.get('lastObject'));
            console.log("lastHealth is", _self.get("lastHealth"));
        });
      }
    }.observes("itemtypeCode"),
    doQuery: function() {
        //alert("doQuery");
        var _self = this;
        var filter = {};
        var flag = this.get("flag");
        var sort;
        //按照id来查询
        if (this.get('customerInComp.id')) {
            filter = $.extend({}, filter, {
                '[examUser][id]': this.get('customerInComp.id')
            });
        }
        //按时间日期查询
        if (flag) {
            var compareDate = null;
            if (flag == "today") {
                compareDate = this.get("dateService").getTodayTimestamp();
            }
            if (flag == "seven") {
                compareDate = this.get("dateService").getDaysBeforeTimestamp(7);
            }
            if (flag == "thirty") {
                compareDate = this.get("dateService").getDaysBeforeTimestamp(30);
            }
            filter = $.extend({}, filter, {
                'examDateTime@$gte': compareDate
            });
            //console.log("compareDate is", compareDate);
        }
        //自定义日期
        var conBeginDate = this.get("beginDate");
        console.log("++++conBeginDate+++++", conBeginDate);
        var conEndDate = this.get("endDate");
        if (conBeginDate && conEndDate) {
            var beginCompare = this.get("dateService").getFirstSecondStampOfDay(conBeginDate);
            console.log("beginCompare is", beginCompare);
            var endCompare = this.get("dateService").getLastSecondStampOfDay(conEndDate);
            filter = $.extend({}, filter, {
                'examDateTime@$gte': beginCompare
            }, {
                'examDateTime@$lte': endCompare
            });
            this.set("compareShow", true);
            //this.set("allDateShow",false);
            this.set("todayShow", false);
            this.set("sevenShow", false);
            this.set("thirtyShow", false);
        }
        console.log("filter is", filter);
        //按体检项目查询
        if (this.get('itemtypeID')) {
            //{filter:{'[examUser][id]':this.get('customerInComp.id')}};
            filter = $.extend({}, filter, {
                '[itemtype][id]': this.get('itemtypeID')
            });
        }
        this.get("store").query('health-info', {
            sort: {
                '[examDateTime]': 'asc'
            },
            filter: filter,
        }).then(function(healthList) {
            _self.set('healthList', healthList);
            //_self.set('firstHealth',healthList.get('firstObject'));
            //console.log('firstHealth is',_self.get('firstHealth'));
            //_self.set('lastHealth',healthList.get('lastObject'));
            //console.log('lastHealth is',_self.get('lastHealth'));
            console.log("isisis healthList", healthList);
            _self.showChartData();
        });
        if (!this.get('itemtypeID')) {
            this.set("informationShow", true);
        }
        if (!flag && !(conBeginDate && conEndDate)) {
            this.set("allDateShow", true);
        }
        //查询最后一次血压
    },
    didInsertElement: function() {
        var _self = this;
        this.doQuery();
        // this.get("store").query('health-info', {
        //     filter: {
        //         '[itemtype][typecode]': 'healthExamType1'
        //     },
        //     sort: {
        //         '[examDateTime]': 'asc'
        //     },
        // }).then(function(bloodList) {
        //     _self.set('lastHealth', bloodList.get('lastObject'));
        //     console.log("lastHealth is", _self.get("lastHealth"));
        // });
    },
    showChartData() {
        var _self = this;
        var healthList = this.get("healthList");
        var myChart = _self.get("myChart");
        var seriesData = [];
        var xAxisData = [];
        var seriesAddtionData = [];
        var chartShow="";
        var chartAddition="";
        var itemtypeName="";
        var itemTypeAddition="";
        var createSeries=[];
        var typename="";
        healthList.forEach(function(health) {
            var examDateTime = health.get("examDateString");
            var result = health.get("result");
            var resultAddtion = health.get("resultAddtion");
            if (_self.get('itemtypeID')) {
                xAxisData.push(examDateTime);
                seriesData.push(result);
                seriesAddtionData.push(resultAddtion);
          } else {}
          if(_self.get("itemtypeCode")=='healthExamType1'){
            console.log("showChartData itemtypeCode",_self.get("itemtypeCode"));
            chartShow="高压";
            chartAddition="低压";
            itemtypeName="高压";
            itemTypeAddition="低压";
            typename=health.get("itemtype.typename");
          }else {
            chartShow='';
            chartAddition='';
            //itemtypeName=_self.get("itemtype.typename");
            typename=health.get("itemtype.typename");
            itemtypeName=health.get("itemtype.typename");
            console.log("itemtypeName is",itemtypeName);
            itemTypeAddition="";
          }
        });
        // 绘制图表
        myChart.setOption({
            title: {
                text: '体检项目统计表'+"-"+typename
            },
            tooltip: {
                trigger: "axis"
            },
            legend: {
                data: [chartShow, chartAddition],
                y: "top"
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [{
                type: 'category', //类目轴,选择此项必须使用data设置数据
                boundaryGap: false,
                data: xAxisData,
                axisLabel: {
                    interval: 0
                }
            }],
            yAxis: {
                name: 'mmHg',
                type: 'value',
            },
            dataZoom: [{ // 这个dataZoom组件，默认控制x轴。
                type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
                start: 10, // 左边在 10% 的位置。
                end: 90, // 右边在 60% 的位置。
                filterMode: 'filter'
            }],
            series: [{
                name: itemtypeName,
                type: "line",
                smooth: true,
                itemStyle: {
                    normal: {
                        lineStyle: {
                            color: "#ff0000"
                        }
                    }
                },
                //stack:"",
                data: seriesData
            }, {
                name: itemTypeAddition,
                type: "line",
                smooth: true,
                data: seriesAddtionData
            }]
        });
    },
    initChart() {
        if (this.get("hasInitChart")) {
            return;
        }
        this.set("hasInitChart", true);
        var pw = $("#health-area").width();
        console.log("pw is:" + pw);
        $("#myChart").width(pw);
        var myChart = Echarts.init(document.getElementById('myChart'));
        console.log("#myChart", document.getElementById("my"));
        this.set("myChart", myChart);
    },
    actions: {
        dpShowAction(e) {},
        search: function(flag) {
            //alert("执行了");
            var _self = this;
            this.set("flag", flag);
            this.doQuery();
            this.set("beginDate", null);
            this.set("endDate", null);
            this.set("bedTypeID", '');
            if (flag) {
                if (flag == "today") {
                    this.set("todayShow", true);
                    this.set("sevenShow", false);
                    this.set("thirtyShow", false);
                    this.set("compareShow", false);
                    this.set("allDateShow", false);
                }
                if (flag == "seven") {
                    this.set("todayShow", false);
                    this.set("sevenShow", true);
                    this.set("thirtyShow", false);
                    this.set("compareShow", false);
                    this.set("allDateShow", false);
                }
                if (flag == "thirty") {
                    this.set("todayShow", false);
                    this.set("sevenShow", false);
                    this.set("thirtyShow", true);
                    this.set("compareShow", false);
                    this.set("allDateShow", false);
                }
            }
        },
        //添加按钮
        addData: function() {
            var _self = this;
            this.set('showpopInvitePassModal', true);
            let health = this.get("store").createRecord('health-info', {});
            this.set('health', health);
            this.set("bloodShow", false);
            this.set("heartShow", false);
        },
        //弹窗取消
        invitation() {
            this.set('showpopInvitePassModal', false);
        },
        //保存按钮
        saveClick: function() {
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            //alert("保存");
            this.doQuery();
            this.set("showpopInvitePassModal", false);
            var _self = this;
            var mainpageController = App.lookup('controller:business.mainpage');
            let health = this.get("store").createRecord('health-info', {});
            //health.set('examDateTime', _self.get("health.examDateTime"));
            health.set('itemtype', _self.get("health.itemtype"));
            health.set('result', _self.get("health.result"));
            console.log("health result",_self.get("health.result"));
            health.set("examUser", _self.get("customerInComp"));
            var examDateTime = _self.get("health.examDateString");
            var examDate = _self.get("dateService").timeStringToTimestamp(examDateTime);
            health.set("examDateTime", examDate);
            console.log("health save",health);
            health.save().then(function() {
                //alert("跳转");
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
            });
        },
        showList: function() {
            this.set('textShow', true);
            $('#listBtn2').attr('class', 'cur');
            $('#textBtn2').attr('class', '');
            //this.initChart();
        }, //列表显示
        showText: function() {
            this.set('textShow', false);
            this.initChart();
            this.showChartData();
            $('#listBtn2').attr('class', '');
            $('#textBtn2').attr('class', 'cur');
        }, //图文显示
        showDate: function() {
            this.set('dateShow', true);
        }, //显示时间选择器
        hideDate: function() {
            this.set('dateShow', false);
            this.send('search');
        }, //隐藏时间选择器
        showBigImg: function() {
            this.set('bigImg', true);
        }, //大图显示
        hideBigImg: function() {
            this.set('bigImg', false);
        }, //大图显示
        newData: function() {
            $('#windowInsideDIV').modal();
        }, //新增健康数据弹窗
        hideData: function() {
            $('#windowInsideDIV').hide();
        },
        healthSelect: function(itemtype) {
            if (itemtype) {
               this.set('itemtype', itemtype);
                this.set('itemtypeID', itemtype.id);
                this.set('itemtypeCode',itemtype.get('typecode'));
                console.log("itemtypeCode is",this.get('itemtypeCode'));
                //alert("false");
                this.set("informationShow", false);
                console.log("informationShow itemtype", this.get("informationShow"));

            } else {
                this.set('itemtypeID', '');
                //alert("true");
                if (!this.get("textShow")) {
                    this.set("informationShow", true);
                }
                console.log("informationShow kong", this.get("informationShow"));
            }
            //this.send('search');
            this.doQuery();
        }, //数据项字典
        addHealthSelect: function(healthDict) {
            this.get("health").set("itemtype", healthDict);
            var healthDictId = healthDict.get("id");
            if (healthDictId == "152") {
                this.set("bloodShow", true);
            } else {
                this.set("bloodShow", false);
            }
            if (healthDictId == "154") {
                this.set("heartShow", true);
            } else {
                this.set("heartShow", false);
            }
        }, //添加界面数据项字典
        changeExamAction(date) {
            var stamp = this.get("dateService").getLastSecondStampOfDay(date);
            this.set("health.examDateTime", stamp);
        },
        changeBeginDateAction(date) {
            this.set("beginDate", date);
            var begin = this.get("dateService").timeToTimestamp(date);
            var computedBegin = this.get("dateService").formatDate(begin, "yyyy-MM-dd");
            this.set("computedBegin", computedBegin);
            if (date) {
                this.set("todayShow", false);
                this.set("sevenShow", false);
                this.set("thirtyShow", false);
                this.set("compareShow", true);
                this.set("allDateShow", false);
            }
        },
        changeEndDateAction(date) {
            //console.log("is true",typeof date);
            this.set("endDate", date);
            var end = this.get("dateService").timeToTimestamp(date);
            var computedEnd = this.get("dateService").formatDate(end, "yyyy-MM-dd");
            this.set("computedEnd", computedEnd);
            if (date) {
                this.set("todayShow", false);
                this.set("sevenShow", false);
                this.set("thirtyShow", false);
                this.set("compareShow", true);
                this.set("allDateShow", false);
            }
        },
    }
});
